package com.ms.game.slot.base;

import java.util.ArrayList;

public class SlotScatterCounter {
	private ArrayList<Scatter> list;
	
	public ArrayList<Scatter> getScatterBingo(ArrayList<Integer> bingosymbol , int minBingoSize  ,  ArrayList<Integer> screen)
	{
		list = new ArrayList<Scatter>();
		
		for(int id:bingosymbol)
		{
					int combo = 0;
					ArrayList<Integer> screenidx = new ArrayList<Integer>();
					for(int i=0;i<screen.size();i++)
					{
						if(id ==  screen.get(i))
						{
							combo++;
							screenidx.add(i);
						}
					}
					
					if(combo >=minBingoSize)
					{
						Scatter scatter = new Scatter();
						scatter.symbol = id;
						scatter.count = combo;
						scatter.screenidx = screenidx;
						
						list.add(scatter);
					}
		}
		
		
		return list;
	}
	
	/**
	 * 專門對某個symbol做scatter(針對規定顆數)
	 * @param bingosymbol
	 * @param minBingoSize
	 * @param screen
	 * @return
	 */
	public ArrayList<Scatter> getScatterBingoBySingle(ArrayList<Integer> bingosymbol , int minBingoSize  ,  ArrayList<Integer> screen)
	{
		list = new ArrayList<Scatter>();
		for(int id:bingosymbol)
		{
			for(int i=0;i<screen.size();i++)
			{
				if(id ==  screen.get(i))
				{
					Scatter scatter = new Scatter();
					scatter.symbol = id;
					scatter.count = minBingoSize;
					scatter.screenidx = new ArrayList<Integer>();
					scatter.screenidx.add(i);
					list.add(scatter);
				}
			}
		}
		
		return list;
	}
	
	/**
	 * 取得大型狀的scatter中獎<br>
	 * 1. 盤面的寬度一定要是中獎symbol的倍數，而且邊緣要切齊<br>
	 * 2. scatter、盤面本身要是矩形(每一輪長度相同)
	 */
	public ArrayList<Scatter> getBigSymbolBingoScatter
	(
			ArrayList<int[][]> bingoScatterSymbols,
			ArrayList<Integer> screenSymbols,
			int minBingoSize,
			int screenColSize,
			int screenRowSize
	)
	{
		list = new ArrayList<Scatter>();
		
		for(int[][] symbol : bingoScatterSymbols){
			
			//以最左上角的symbol為symbolID
			int scatterSymbol = symbol[0][0];
			//圖標的寬度
			int scatterSymbolColSize = symbol.length;
			//圖標的第一欄長度
			int scatterFirstColRowSize = symbol[0].length;
			
			//中獎的位置
			ArrayList<Integer> bingoIDXs = new ArrayList<Integer>();
			
			for(int col = 0; col < screenColSize; col++){
				
				//是對齊的第一欄
				if(col % scatterSymbolColSize == 0){
					
					int startIDX = col * screenRowSize;
					int endIDX = startIDX + screenRowSize;
					
					//對這一欄的所有列
					for(int idx = startIDX; idx < endIDX; idx++){
						
						//盤面上的symbol
						int screenSymbol = screenSymbols.get(idx);
						
						//如果是左上角的symbol
						if(screenSymbol == scatterSymbol){
							bingoIDXs.add(idx);
							
							//跳過一個scatter第一欄長度
							idx += (scatterFirstColRowSize - 1);
						}
						
					}
					
					//如果是長條的scatter，補判斷最上面的是不是scatter的其他symbol
					if(scatterFirstColRowSize > 1){
						int topScreenSymbol = screenSymbols.get(startIDX);
						
						for(int row = 1; row < scatterFirstColRowSize; row++){
							if(topScreenSymbol == symbol[0][row]){
								bingoIDXs.add(startIDX);
								break;//只要找到任何一個的symbol，就代表頂端也是一個中獎位置
							}
						}
	
					}
					
				}
			}
			
			//有中獎
			if(bingoIDXs.size() >= minBingoSize){
				
				Scatter scatter = new Scatter();
				scatter.symbol = scatterSymbol;
				scatter.screenidx = bingoIDXs;
				scatter.count = bingoIDXs.size();
				
				list.add(scatter);
			}
		}
		
		return list;	
	}
	
	public static void main(String[] args) 
	{
//		//4096 way
//		Integer[] screen_symbol = 
//		{
//				0,1,1,2,1,
//				1,2,1,3,0,
//				3,1,3,1,1,
//				1,3,1,2,3
//		};
//		Integer[] bingo_symbol = {2,3};
//		
//		List<Integer> ss = Arrays.asList(screen_symbol);
//		List<Integer> bs = Arrays.asList(bingo_symbol);
//		
//		ArrayList<Integer> screen = new ArrayList<Integer>(ss);
//		ArrayList<Integer> bingosymbol = new ArrayList<Integer>(bs);
//		
//	
//		
//		long starttime = TimeSys.currentTimeMills();
//		
//		
//		
//		
//		SlotScatterCounter counter = new SlotScatterCounter();	
//		
//		ArrayList<int[][]> scattes = new ArrayList<int[][]>();
//		scattes.add(HOLMESSymbolType.BONUS.getGroupSymbols());
//		
//		Integer[] screen = 
//		{
//				20,20,0,20,2001,0,
//				20,20,0,2002,2003,0,
//				2001,0,0,0,0,0,
//				2003,0,0,0,0,0,
//				0,20,2001,0,0,0,
//				0,2002,2003,0,0,0
//		};
//		ArrayList<Integer> screenSymbols = new ArrayList<Integer>(Arrays.asList(screen));
//		
//		
//		ArrayList<Scatter> result = counter.getBigSymbolBingoScatter
//				(
//						scattes, screenSymbols, 3, 6, 6
//				);
//		
//		if(result.size() > 0){
//			for(Scatter scatter : result){
//				System.out.println(scatter.symbol+"x"+scatter.count+scatter.screenidx.toString());
//			}
//		}
//		else{
//			System.out.println("NONE");
//		}
//		
//		long end = TimeSys.currentTimeMills() - starttime;
//		System.out.println("waste time = "+end+" ms");
	}
}
